package com.Akronmcp.hb;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;

import com.Akronmcp.hta.obj.HManager;
import com.Akronmcp.hta.obj.HopBook;
import com.Akronmcp.hta.obj.HopChat;


public class HbLoader {

	static FileConfiguration conf = HbM.conf;
	static FileConfiguration lang = HbM.lang;
	static HManager hta = HbM.hta;
	
	public static File df = HbM.df;
	
	public static Map<UUID,ReadInfo> RI = new LinkedHashMap<UUID,ReadInfo>();
	
	public static Map<String,String> cmds = new LinkedHashMap<>();
	public static Map<Integer,String> items = new LinkedHashMap<>();
	
	/**载入插件配置*/
	public static void reload_conf() throws Exception
	{
	 	 hta = HbM.hta;
	
		 conf = null;
         lang = null;
         //清空
         
         conf = YamlConfiguration.loadConfiguration(new File(df,"config.yml"));
        
         String langtype = conf.getString("langtype");
    	 lang = YamlConfiguration.loadConfiguration(new File(df+"//lang",langtype+".yml"));
        //载入语言
         HbM.lang = lang;
		//确定指针
		
         String in = conf.getString("input");
         //载入文件列表
         
    	 hta.clearAll();
    	 
         if(in!=null)
         {
        	 String ins[] = in.split("\\n");
        	 //生成文件名列表
        	 
        	 for(String file:ins)
        	 {//遍历载入hta对象
        		 hta.load(new File(df,file.trim()));
        	 }
        	 hta.initAllHopChat();
        	 hta.initAllHopBook();
        	 hta.initAllHopHolo();
         }
         
         ConfigurationSection set = conf.getConfigurationSection("settings");
         for(String id:set.getKeys(false))
         {
        	 String oc = set.getString(id+".open_cmd");
        	 int item = set.getInt(id+".item",-1);
        	 
        	 if(oc!=null)
        		 cmds.put(oc,id);
        	 if(item!=-1)
        		 items.put(item,id);
         }
         
	}
	
	public static class History{
		HopBook book;
		String page;
	}
	
	public static class ReadInfo
	{
		HopBook curbook;
		
		List<History> history = new ArrayList<History>();
		int hindex;
		
		String current;
		int index;
		
		
		public ReadInfo(HopBook hb)
		{
			curbook = hb;
		}
		
		public void setHis(List<History> history) 
		{
			this.history = history;
		}
		
		public String getHBId()
		{
			return curbook.getId();
		}
		public HopChat turnTo(HopBook hb,int page)
		{
			curbook = hb;
			return view(page);
		}
		
		public HopChat turnTo(HopBook hb,String page)
		{
			curbook = hb;
			return view(page);
		}
		
		HopChat getPage(String page) {
			return curbook.getPages().get(page);
		}
		HopChat getPage(int page) {
			int i=0;
			for(HopChat o:curbook.getPages().values())
			{
				if(i==page)return o;
				i++;
			}
			return null;
		}
		
		int getIndex(String page)
		{
			int i=0;
			for(String o:curbook.getPages().keySet())
			{
				if(o.equals(page))return i;
				
				i++;
			}
			return 0;
		}
		
		public HopChat view(String page)
		{
			HopChat pa = getPage(page);
			
			if(pa==null)return null;
			
			current = page;
			putHistory(curbook,page);
			index = getIndex(page);
			
			return pa;
		}
		
		public HopChat next()
		{
			hindex++;
			if(hindex>=0&&hindex<history.size())
			{
				History hi = history.get(hindex);
				if(hi.book.equals(curbook))
				{
					return getPage(hi.page);
				}else
				{
					return turnTo(hi.book,hi.page);
				}
			}
			return null;
		}
		public HopChat back()
		{
			hindex--;
			if(hindex>=0&&hindex<history.size())
			{
				History hi = history.get(hindex);
				if(hi.book.equals(curbook))
				{
					return getPage(hi.page);
				}else
				{
					return turnTo(hi.book,hi.page);
				}
			}
			return null;
		}
		
		public HopChat view()
		{
			if(current==null)return view(0);
			
			HopChat pa = getPage(current);
			if(pa==null)pa = view(0);
			return pa;
		}
		
		public HopChat view(int page)
		{
			HopChat pa = getPage(page);
			if(pa==null)return null;
			current = pa.getId();
			putHistory(curbook,pa.getId());
			index = page;
			
			return pa;
		}

		public HopChat nextPage()
		{
			return view(index+1);
		}
		public HopChat prePage()
		{
			return view(index-1);
		}
		
		public void putHistory(HopBook b,String p)
		{
			history.add(new History() {{book=b;page=p;}});
			if(history.size()>10)
				history.remove(0);
			
			hindex = history.size()-1;
		}
		
		
		public void see(Player p,HopChat htx)
		{
			if(!p.isOnline())
			return;
			
			String perm = conf.getString("settings."+curbook.getId()+".permission");
			if(perm!=null)
			if(!p.hasPermission(perm))
			{
				back();
				p.sendMessage(HbComer.help("noperm"));
				return;
			}
			
			ItemStack book = null;
			
			if(!curbook.isSep())
			{
				book = curbook.getItemBook();
			}else
			{
				ItemStack tbo = new ItemStack(Material.WRITTEN_BOOK,1);
				BookMeta im2 = (BookMeta) tbo.getItemMeta();
				im2.addPage("233");
				tbo.setItemMeta(im2);
				book = curbook.createBy(Arrays.asList(htx),tbo,p);
			}
				
			int slot = p.getInventory().getHeldItemSlot();
			ItemStack old = p.getInventory().getItem(slot);
			p.getInventory().setItem(slot,book);
			HopBook.letOpen(p);
			p.getInventory().setItem(slot,old);
		}
		
		
	}
	
}
